package com.ai.emall.business.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.emall.bean.gbean.MallProductModule;
import com.ai.emall.bean.gbean.Product;
import com.ai.emall.bean.gbean.ProductPicture;
import com.ai.emall.bean.gbean.ProductSku;
import com.ai.emall.business.interfaces.IProductManageQuerySV;
import com.ai.emall.constants.EmallBusiConstants;
import com.ai.emall.dao.gdao.ProductMapper;	
import com.ai.emall.dao.gdao.ProductPictureMapper;
import com.ai.emall.service.interfaces.IMallProductModuleSV;
import com.ai.emall.service.interfaces.IProductSkuSV;
import com.ai.emall.utils.constants.SysExceptCodeConstants;
import com.ai.emall.utils.exception.BusinessException;
import com.ai.emall.utils.util.CollectionUtil;
import com.ai.emall.utils.util.StringUtil;
import com.ai.gcf.api.base.vo.PageInfo;
import com.ai.gcf.api.emall.productmanage.param.ProductInfo;

/**
 * @author zhaowg3
 * @Date 2016年12月20日
 */
@Service
public class ProductManageQuerySVImpl implements IProductManageQuerySV {
	@Autowired
	private ProductPictureMapper productPictureMapper;
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private IMallProductModuleSV iMallProductModuleSV;
	@Autowired
	private IProductSkuSV iProductSkuSV;
	@Override
	public List<ProductInfo> productQueryByModuleId(String moduleId, String status) throws Exception {
		List<ProductInfo> productList = new ArrayList<ProductInfo>();
		// 先在模块表中查询该模块下的排好序的所有商品
		List<MallProductModule> moduleProducts = iMallProductModuleSV.getBeansByModuleIdAndStatus(moduleId, status) ;
		if (!CollectionUtil.isEmpty(moduleProducts)){
			// 查询到商品
			int sort = 1;
			for (MallProductModule moduleProduct : moduleProducts) {
				ProductInfo productInfo = this.queryDealProductInfo(moduleProduct.getProductId(),moduleProduct.getPictId());
				productInfo.setModuleId(moduleProduct.getModuleId());
				productInfo.setSort(sort);
				sort++;
				productList.add(productInfo);
			}
		}
		return productList;
	}
	
	@Override
	public ProductInfo productQueryByProductId(Long productId) throws Exception {
		// 先在模块表中查询该模块下的排好序的所有商品
		List<MallProductModule> moduleProducts = iMallProductModuleSV.getBeansByProductId(productId);
		if (CollectionUtil.isEmpty(moduleProducts))
			throw new BusinessException(SysExceptCodeConstants.Special.NO_RESULT,"根据productId:" + productId + "未查询到商品");
		// 查询到商品
		ProductInfo productInfo = this.queryDealProductInfo(moduleProducts.get(0).getProductId(),moduleProducts.get(0).getPictId());
		return productInfo;
	}
	
	/**
	 * 查询某个产品的详细信息
	 * @param productId
	 * @param picId
	 * @return
	 * @throws Exception
	 * @author zhaowg3
	 * @Date 2016年12月31日
	 */
	private ProductInfo queryDealProductInfo(Long productId,Long picId) throws Exception {
		ProductInfo productInfo = new ProductInfo();
		// 查询商品图片
		ProductPicture productPic = productPictureMapper.selectByPrimaryKey(picId);
		if (productPic ==null)
			throw new BusinessException(SysExceptCodeConstants.Special.NO_RESULT,"根据picId:" + picId + "没有查询到图片信息");
		productInfo.setProductPictureDir(productPic.getVfsId());
		// 开始在商品表中查询
		Product product = productMapper.selectByPrimaryKey(productId);
		if (product == null)
			throw new BusinessException(SysExceptCodeConstants.Special.NO_RESULT,"根据productId:" + productId + "没有查询到商品信息");
		productInfo.setProductName(product.getName());
		productInfo.setProductLink(product.getDrainageUrl());
		productInfo.setProductId(product.getProductId());
		// 商品对应国美侧的productId和SkuId
		String param = product.getParam();
		if(StringUtil.isBlank(param))
			throw new BusinessException(SysExceptCodeConstants.Special.NO_RESULT,"根据productId:" + productId + "没有查询到商品的真实productId和skuId");
		String[] params = param.split(EmallBusiConstants.Product.PARAM_SPLIT);
		if (!CollectionUtil.isEmpty(params) && params.length == 2) {//param字段格式不正确
			productInfo.setGmProductId(params[0]);
			productInfo.setGmSkuid(params[1]);
		}else{
			productInfo.setGmProductId("");
			productInfo.setGmSkuid("");
		}
		// 查询sku表
		List<ProductSku> productSkus = iProductSkuSV.getBeansByProductId(productId);
		if (!CollectionUtil.isEmpty(productSkus)){
			ProductSku productSku = productSkus.get(0);
			productInfo.setPriceType(productSku.getPriceMode());
			productInfo.setNormalPrice(productSku.getNormalPrice());
			productInfo.setSailPrice(productSku.getSailPrice());
			productInfo.setHighestComm(productSku.getHighestComm());
		}
		return productInfo;
	}

	@Override
	public PageInfo<ProductInfo> productQueryByModuleId(String moduleId, String status, Integer pageNo,
			Integer pageSize) throws Exception {
		List<ProductInfo> productList = new ArrayList<ProductInfo>();
		PageInfo<ProductInfo> info = new PageInfo<ProductInfo>();
		info.setPageNo(pageNo);
		info.setPageSize(pageSize);
		info.setResult(productList);
		// 先在模块表中查询该模块下的排好序的所有商品
		com.github.pagehelper.PageInfo<MallProductModule> moduleProducts = iMallProductModuleSV.getBeansByModuleIdAndStatus(moduleId, status,pageNo,pageSize) ;
		info.setCount((int)moduleProducts.getTotal());
		int sort = 1;
		for (MallProductModule moduleProduct : moduleProducts.getList()) {
			ProductInfo productInfo = this.queryDealProductInfo(moduleProduct.getProductId(),moduleProduct.getPictId());
			productInfo.setModuleId(moduleProduct.getModuleId());
			//解决每页重新排序的问题
			productInfo.setSort((pageNo - 1) * 10 + sort);
			sort++;
			productList.add(productInfo);
		}
		return info;
	}


}
